<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/27 0027
 * Time: 下午 7:29
 * Administrator
 */

namespace app\common\service;


use Qcloud\Cos\Client;

class QcloudCosUpload extends BaseService
{

    public static function upload($local_path,$file_name=null)
    {
        trace($local_path,'post.file.url');
        $cosClient = new Client([
            'region' => SysConfigService::getSysConfig('qcloud.cos.region'),
            'credentials'=> [
                'appId' => SysConfigService::getSysConfig('qcloud.cos.appid'),#getenv('COS_APPID'),
                'secretId'    => SysConfigService::getSysConfig('qcloud.cos.secretId'),#getenv('COS_KEY'),
                'secretKey' => SysConfigService::getSysConfig('qcloud.cos.secretKey')]]);#getenv('COS_SECRET')

        if (!$file_name){
            $paths = explode('/',$local_path);
            $files = explode('.',$paths[count($paths)-1]);
            $file_name = md5(time()).'.'.$files[1];
        }
        ### 上传文件流
        try {
            $result = $cosClient->putObject(array(
                'Bucket' => SysConfigService::getSysConfig('qcloud.cos.bucket.vopic'),
                'Key' => $file_name,
                'Body' => fopen($local_path, 'rb')));
            trace($result->toArray(),'post.file.result');
            return $result->toArray();
        } catch (\Exception $e) {
            throw $e;
        }
    }
}